package yxy.game.pm2.command.moudleactive.rankprogress;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.rankschedule;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ModuleActiveMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.active.progress.RankProgress;
import yxy.game.pm2.module.active.progress.RankProgressArea;
import yxy.game.pm2.module.active.progress.RankProgressPerson;

import java.util.Collection;
import java.util.HashMap;

/** 模块活动 排行榜进度 */
@MessageType(proto = Command.RankProgressRewardDraw, classtype = ModuleActiveMessage.RankProgressRewardDrawRequest.class)
public class CMD_RankProgressRewardDraw extends Command<ModuleActiveMessage.RankProgressRewardDrawRequest> {

	@Override
	public void execute(MessageSource messageSource, ModuleActiveMessage.RankProgressRewardDrawRequest request) {
		String playerCode = messageSource.getTag();
		int type = request.getType();
		int id = request.getId();
		ModuleActiveMessage.RankProgressRewardDrawResponse.Builder response = ModuleActiveMessage.RankProgressRewardDrawResponse.newBuilder();
		response.setType(type);
		
		RankProgressArea moudlearea = RankProgressArea.get(playerCode, type);
		RankProgressPerson moudleperson = RankProgressPerson.get(playerCode, type);
		
		HashMap<Integer, RankProgress> idcodemap = moudlearea.getRpmap();
		
		if(moudleperson.getDrawids().contains(id)) {
			send(messageSource, response, Result.ActHadGet);
			return;
		}
		
		if(!idcodemap.containsKey(id)) {
			send(messageSource, response, Result.ActCannotGet);
			return;
		}
		
		rankschedule rs = DataCenter.getData(id, rankschedule.class);
		if(rs == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		Collection<Currency> curs = Currency.create(rs.getRewardtype(), rs.getRewardid(), rs.getRewardnum());
		Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
		response.addAllRewards(items);
		
		moudleperson.getDrawids().add(id);
		moudleperson.set();
		
		new CurrencyHandle().addCurrency(playerCode, CCReason.排行榜进度, curs);
		
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ModuleActiveMessage.RankProgressRewardDrawResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}